package com.jlu.scoreSystem;

import java.util.*;

public class Operator {
    ArrayList<Student> students = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    public void start(){
        while (true) {
            System.out.println("====学生成绩系统====");
            System.out.println("所有操作如下：");
            System.out.println("1,输入学生信息与成绩");
            System.out.println("2,计算每个学生的平均分与总分");
            System.out.println("3,计算班级平均分与最高分");
            System.out.println("4,按成绩排序并显示学生排名");
            System.out.println("5,查找特定学生的成绩信息");
            System.out.println("请选择对应操作之前的序号：");

            String command = sc.next();
            switch (command){
                case "1":
                    scan();
                    break;
                case "2":
                    averageOverall();
                    break;
                case "3":
                    highestOverall();
                    break;
                case "4":
                    ranking();
                    break;
                case "5":
                    display();
                    break;
                default:
                    System.out.println("您输入有误~~请重新输入~~");
            }
        }
    }


    public void scan(){
        Student student = new Student();

        System.out.println("====开始输入学生信息====");
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        student.setName(name);
        System.out.println("请输入学生学号：");
        String studentID = sc.next();
        for (int i = 0; i < students.size(); i++) {
            if(Objects.equals(students.get(i).getStudentID(), studentID)){
                while (true) {
                    System.out.println("该学生已存在~~是否需要更换该学生的信息？若需要请输入“需要”");
                    String command = sc.next();
                    if (command.equals("需要")) {
                        scoreScanner(students.get(i));
                        return;
                    }
                }
            }
        }
        student.setStudentID(studentID);
        scoreScanner(student);
        students.add(student);
    }

    public void scoreScanner(Student student){
        while (true) {
            System.out.println("请输入学生数学成绩：");
            double mathScore = sc.nextDouble();
            if (mathScore >= 0.0 && mathScore <= 100.0){
                student.setMath(mathScore);
                break;
            }else{
                System.out.println("您输入的成绩不在0~100分区间内~~请重新输入~~");
            }
        }

        while (true) {
            System.out.println("请输入学生英语成绩：");
            double englishScore = sc.nextDouble();
            if (englishScore >= 0.0 && englishScore <= 100.0){
                student.setEnglish(englishScore);
                break;
            }else{
                System.out.println("您输入的成绩不在0~100分区间内~~请重新输入~~");
            }
        }

        while (true) {
            System.out.println("请输入学生地理信息系统原理成绩：");
            double gisScore = sc.nextDouble();
            if (gisScore >= 0.0 && gisScore <= 100.0){
                student.setGis(gisScore);
                break;
            }else{
                System.out.println("您输入的成绩不在0~100分区间内~~请重新输入~~");
            }
        }
    }

    public void averageOverall(){
        if (students.isEmpty()){
            System.out.println("还没有学生信息呢，请输入后再来吧~~");
            return;
        }

        for (int i = 0; i < students.size(); i++) {
            double average = ( students.get(i).getMath() + students.get(i).getGis() + students.get(i).getEnglish() ) / 3.0;
            double overall = students.get(i).getMath() + students.get(i).getGis() + students.get(i).getEnglish();
            System.out.println(students.get(i).getName() + "同学三科的平均分为：" + average + ",总分为：" + overall);
        }
    }

    public void highestOverall() {
        if (students.isEmpty()){
            System.out.println("还没有学生信息呢，请输入后再来吧~~");
            return;
        }

        double max =  students.get(0).getMath() + students.get(0).getGis() + students.get(0).getEnglish();
        double temp = 0.0;
        for (int i = 0; i < students.size(); i++) {
            if(max < students.get(i).getMath() + students.get(i).getGis() + students.get(i).getEnglish()){
                max = students.get(i).getMath() + students.get(i).getGis() + students.get(i).getEnglish();
            }
            temp += (students.get(i).getMath() + students.get(i).getGis() + students.get(i).getEnglish());
        }
        System.out.println("该班级平均分为：" + temp / students.size() + ",最高分为：" + max);
    }

    public void ranking() {

        if (students.isEmpty()){
            System.out.println("还没有学生信息呢，请输入后再来吧~~");
            return;
        }

        students.sort(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                double totalScore1 = s1.getMath() + s1.getEnglish() + s1.getGis();
                double totalScore2 = s2.getMath() + s2.getEnglish() + s2.getGis();
                return Double.compare(totalScore2, totalScore1);
            }
        });
        System.out.println("====以下为成绩排名====");
        for (Student student : students) {
            double totalScore = student.getMath() + student.getEnglish() + student.getGis();
            System.out.println(student.getName() + " - 总分: " + totalScore);
        }
    }


    public void display(){
        System.out.println("请输入您想查找的学生的学号：");
        String searchID = sc.next();
        for (int i = 0; i < students.size(); i++) {
            if(searchID.equals(students.get(i).getStudentID())){
                System.out.println("==============================");
                System.out.println("以下是学号为" + "的同学的信息：");
                System.out.println("姓名：" + students.get(i).getName());
                System.out.println("数学成绩：" + students.get(i).getMath());
                System.out.println("英语成绩：" + students.get(i).getEnglish());
                System.out.println("地理信息系统原理成绩：" + students.get(i).getGis());
                System.out.println("==============================");
                return;
            }
        }
        System.out.println("未查询到该学生信息~~~");
    }

}
